package com.api.shopapi.mapper;

import com.api.shopapi.entity.PracticeSite;
import com.api.shopapi.entity.VolunteerTeam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface PracticeSiteMapper {

    @Insert("insert into practice_site " +
            "(" +
            "PracticeSiteCode," +
            "UserCode," +
            "Address," +
            "ApplyStatus," +
            "CreateTime," +
            "CommandCenterCode," +
            "GovernmentCode" +
            ") " +
            "values " +
            "(" +
            "#{practiceSite.PracticeSiteCode}," +
            "#{practiceSite.UserCode}," +
            "#{practiceSite.Address}," +
            "#{practiceSite.ApplyStatus}," +
            "#{practiceSite.CreateTime}," +
            "#{practiceSite.CommandCenterCode}," +
            "#{practiceSite.GovernmentCode}" +
            ")" )
    int insert(@Param("practiceSite") PracticeSite practiceSite);


    @Delete("delete from practice_site where practiceSiteCode=#{practiceSiteCode}")
    int delete(@Param("practiceSiteCode") String practiceSiteCode);


    @Update("update practice_site set " +
            " Address=#{practiceSite.Address}," +
            " ApplyTime=#{practiceSite.ApplyTime}," +
            " PassTime=#{practiceSite.PassTime}," +
            " RejectMessage=#{practiceSite.RejectMessage}," +
            " ApplyStatus=#{practiceSite.ApplyStatus}" +
            " where practiceSiteCode=#{practiceSite.practiceSiteCode}")
    int update(@Param("practiceSite") PracticeSite practiceSite);


    @Select("select * from practice_site where practiceSiteCode=#{practiceSiteCode} limit 1")
    PracticeSite getByPracticeSiteCode(@Param("practiceSiteCode") String practiceSiteCode);


    @Select("select * from practice_site where userCode=#{userCode} limit 1")
    PracticeSite getByUserCode(@Param("userCode") String userCode);


    @Select("<script>" +
            "select * from practice_site where commandCenterCode=#{commandCenterCode}" +
            "<if test ='applyStatus != -1 '>" +
            " and ApplyStatus = #{applyStatus} " +
            "</if>" +
            " order by CreateTime desc" +
            "</script>")
    List<PracticeSite> listByCommandCenterCode(@Param("commandCenterCode") String commandCenterCode,
                                             @Param("applyStatus") int applyStatus);


    @Select("select * from practice_site where commandCenterCode=#{commandCenterCode} and ApplyStatus = 1 " +
            " order by ApplyTime desc")
    List<PracticeSite> listByCommandCenterCodeApply(@Param("commandCenterCode") String commandCenterCode);


    @Select("<script>" +
            "select * from practice_site" +
            " where" +
            " commandCenterCode in (select commandCenterCode from command_center where governmentCode=#{governmentCode}) " +
            " order by CreateTime desc" +
            "</script>")
    List<PracticeSite> listByGovCode(@Param("governmentCode") String governmentCode);

}
